package com.hemj.fileInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hemj.common.exception.ServiceException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.hemj.common.utils.SecurityUtils;
import com.hemj.system.service.impl.SysRoleServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hemj.fileInfo.mapper.FileRoleMapper;
import com.hemj.fileInfo.domain.FileRole;
import com.hemj.fileInfo.service.IFileRoleService;
import com.hemj.fileInfo.vo.FileRoleVo;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文件和用户的关联Service业务层处理
 * 
 * @author hemj
 * @date 2024-09-13
 */
@Service
public class FileRoleServiceImpl extends ServiceImpl<FileRoleMapper, FileRole> implements IFileRoleService
{
    @Autowired
    private SysRoleServiceImpl roleService;
    /**
     * 保存权限
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public void saveBatch(FileRoleVo jsonObject){
        if (ObjectUtils.isEmpty(jsonObject.getRoleId())){
            throw new ServiceException("角色ID不能为空");
        }
        LambdaQueryWrapper<FileRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileRole::getRoleId, jsonObject.getRoleId());
        this.remove(queryWrapper);
        List<Long> fileIds = jsonObject.getFileIds();
        List<FileRole> fileRoles = new ArrayList<>();
        for (Long fileId : fileIds){
            FileRole fileRole = new FileRole();
            fileRole.setRoleId(jsonObject.getRoleId());
            fileRole.setFileId(fileId);
            fileRoles.add(fileRole);
        }
        this.saveBatch(fileRoles);
    }


    public List<FileRole>   getRole(FileRoleVo fileRoleVo) {
        if (ObjectUtils.isEmpty(fileRoleVo.getRoleId())){
            throw new ServiceException("角色ID不能为空");
        }
        LambdaQueryWrapper<FileRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FileRole::getRoleId, fileRoleVo.getRoleId());
        List<FileRole> fileRoles = this.list(queryWrapper);
        return fileRoles;
    }

    public List<FileRole> getMyRole() {
        Long userId = SecurityUtils.getUserId();
        List<Long> roleIdList = roleService.selectRoleListByUserId(userId);
        LambdaQueryWrapper<FileRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FileRole::getRoleId, roleIdList);
        List<FileRole> fileRoles = this.list(queryWrapper);
        return fileRoles;
    }


    public Set<Long> getMyFileIds() {
        Long userId = SecurityUtils.getUserId();
        List<Long> roleIdList = roleService.selectRoleListByUserId(userId);
        LambdaQueryWrapper<FileRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FileRole::getRoleId, roleIdList);
        List<FileRole> fileRoles = this.list(queryWrapper);
        Set<Long> fileIds = new HashSet<>();
        for (FileRole fileRole : fileRoles){
            fileIds.add(fileRole.getFileId());
        }
        return fileIds;
    }
}
